package vaani.shabd;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;
import vaani.internal.ActivityType;

/**
 *
 * @author sourcemorph
 */

public class ActivityMiner {

    private static Vector<Vector<String>> verbList;
    private static Vector<ActivityType> activityType;
    private static Vector<Vector<String>> applicationList;

    static {
        verbList = new Vector<Vector<String>>();
        activityType = new Vector<ActivityType>();
        applicationList = new Vector<Vector<String>>();
        try {
            FileInputStream fstream = new FileInputStream("data/analyzerdata.txt");
            BufferedReader reader = new BufferedReader(new InputStreamReader(fstream));
            String temp;
            Vector<String> t = new Vector<String>();
            boolean flag = true;
            while((temp = reader.readLine()) != null) {
                if(temp.compareTo("##") == 0) {
                    if(!flag) {
                        verbList.add(t);
                        t = new Vector<String>();
                    }
                    else
                        flag = false;
                    String x1 = reader.readLine();
                    String [] x = x1.split("/");
                    activityType.add(new ActivityType(Short.parseShort(x[0]), x[1]));
                    Vector<String> tempAppList = new Vector<String>();
                    String [] tempApps = reader.readLine().split(" ");
                    for(String a : tempApps) {
                        tempAppList.add(a);
                    }
                    applicationList.add(tempAppList);
                }
                else {
                    if(temp.compareTo("##!") == 0) {
                        verbList.add(t);
                    }
                    else {
                        t.add(temp);
                    }
                }
            }
            reader.close();
            fstream.close();
        } catch(IOException ex) {
            ex.printStackTrace();
        }
    }

    public static ActivityType matchActivityVerb(String s) {
        for(Vector<String> a : verbList) {
            if(a.contains(s)) {
                return activityType.get(verbList.indexOf(a));
            }
        }
        return null;
    }

    public static Vector<String> getActivityApplication(int x) {
        for(ActivityType a : activityType) {
            if(a.getNum() == x) {
                return applicationList.get(activityType.indexOf(a));
            }
        }
        return null;
    }
}
